﻿/* File Created: 七月 18, 2012 */
/**
* jQuery Ajax 防止重复提交
*/
(function ($) {
    var $ajax = $.ajax;
    $ajax._reuqestsCache = {};
    $ajax._reuqestsCallback = {};

    $.extend({
        postex: function (url, data, callback, options, dtype, ctype) {
            //防止2.5秒内的提交
            var dtDiff = new Date() - new Date($ajax._reuqestsCache[options.index]);
            if (dtDiff < 2500) {
                return false;
            }
            $ajax._reuqestsCache[options.index] = new Date();

            var defcallback = function (response) {
                if (response.indexOf("SUCCESS") != 0) {
                    if (response.toUpperCase().indexOf("FAIL") == 0) {
                        var sShow = response.substring(4);
                        if (sShow != "") {
                            alert(sShow);
                        }
                    } else {
                        alert(response);
                    }
                    return;
                }

                if (response.length > 7) {
                    var sShow = response.substring(7);
                    if (sShow != "") {
                        alert(sShow);
                    }
                }

                //执行输入处理
                callback(response);
            };

            //有带指定应用内容类型的
            if (!!dtype && !!ctype) {
                return $.ajax({
                    url: url,
                    data: data,
                    type: 'POST',
                    contentType: ctype,
                    dataType: dtype,
                    success: defcallback
                });
            }

            if (!!dtype) {
                return $.ajax({
                    url: url,
                    data: data,
                    type: 'POST',
                    dataType: dtype,
                    success: defcallback
                });
            }

            if (!!ctype) {
                return $.ajax({
                    url: url,
                    data: data,
                    type: 'POST',
                    contentType: ctype,
                    success: defcallback
                });
            }

            return $.ajax({
                url: url,
                data: data,
                type: 'POST',
                success: defcallback
            });
        }
    });
})(jQuery)